package jevon.philautus.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import jevon.philautus.domain.User;
import jevon.philautus.service.UserService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 用户控制器
 * 
 * @author fujinjun
 * 
 *         2011-7-8 下午09:10:46
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String index() {
		return "/user/index";
	}

	@RequiresPermissions("user:new")
	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String addNew(Model model) {
		model.addAttribute(new User());
		return "/user/new";
	}
	
	public String create(@Valid User user,BindingResult result){
		if(result.hasErrors()){
			return "/user/new";
		}
		userService.save(user);
		return "redirect:/user";
	}
	
	
}
